package cn.usian.jyzx.user.controller;

import cn.jiyun.pojo.User;
import cn.usian.jyzx.model.dto.h5.UserLoginDto;
import cn.usian.jyzx.model.dto.h5.UserRegisterDto;
import cn.usian.jyzx.model.entity.user.UserAddress;
import cn.usian.jyzx.model.entity.user.UserInfo;
import cn.usian.jyzx.model.vo.common.Result;
import cn.usian.jyzx.model.vo.common.ResultCodeEnum;
import cn.usian.jyzx.model.vo.h5.UserInfoVo;
import cn.usian.jyzx.user.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/user")
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/sms/sendCode/{phone}")
    public Result  sendCode(@PathVariable String phone){
    userService.sendCode(phone);
           return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("userInfo/register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto){
        userService.register(userRegisterDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @PostMapping("userInfo/login")
    public Result login(@RequestBody UserLoginDto userLoginDto){
       String token =  userService.login(userLoginDto);
       return Result.build(token,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("userInfo/auth/getCurrentUserInfo")
    public Result<String> getCurrentUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfoVo userInfoVo = userService.getCurrentUserInfo(token);
        return Result.build(userInfoVo,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("userAddress/auth/findUserAddressList")
    public Result findUserAddressList(HttpServletRequest request){
        String token = request.getHeader("token");
        List<UserAddress> userAddressList = userService.findUserAddressList(token);
        return Result.build(userAddressList, ResultCodeEnum.SUCCESS);
    }
}
